<?php
namespace app\serveradv\logic;

use think\Db;
use \app\manage\logic\BaseLogic;

/**
 * 广告位微应用下的逻辑层
 * @author  蒋哲 2017.5.23
 */
class AdvClassify extends BaseLogic
{
    protected $name = 'adv_classify';
    protected $config = 'db_config_adv';
    protected $advClassifyModel = 'app\serveradv\model\AdvClassify';
    protected $searchWhere = ['name'=>'like','description'=>'like','add_user'=>'like'];


    /*根据id获取其父类的ids数组*/
    public function getParentIds($id): array 
    {
        $list = $this->db()->where(['status'=>['neq',-1]])->select();
        $ids     = $id;
        $params  = 'id';
        $parentIds = parent_ids($list,$ids,$params,$id,0,'pid','id','');
        $ids = explode(",", $ids);
        foreach ($ids as $k => &$v) {
            $v = (int)$v;
        }
        array_splice($ids, array_search(0, $ids), 1);
        return array_reverse($ids);
    }

    /*内部获取某个分类的详情*/
    public function dbCateInfo($data)
    {
        return $this->db()->where(['id'=>$data['id'],'status'=>['neq','-1']])->find();
    }

    /*检验识别码的唯一性*/
    public function checkAction($data): array
    {
        $info = $this->db()->where(['action'=>$data['action']])->find();
        if($info){
            return auto_failure(null, '该识别码action已存在');
        }else{
            return auto_success(null, '识别码有效');
        }
    }

    // 检验父类是否为公共分类
    public function checkIsPublic($pid)
    {
        $_where['id'] = $pid;
        $_where['status'] = array('neq', '-1');
        $_where['is_public'] = array('eq', '1');
        $res = $this->db()->where($_where)->find();
        if ($res === null) {
            return false;
        }
        return true;
    }

    // 广告位分类添加
    public function advClassifyAdd($data)
    {
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        if ($manageAppId != '11') {
            if ($manageAppId != $data['app_id']) {
                return auto_failure ($data,'该分类不属于本应用，无权限添加子分类！');
            }
        }
        if ($data['is_public'] == 1 && $data['pid'] != 0) {
            $checkPublic = $this->checkIsPublic($data['pid']);
            if ($checkPublic === false) {
                return auto_failure ($data,'该分类的父级为私有分类，私有分类下不可创建公共分类！');
            }
        }

        $rst = $this->model($this->advClassifyModel)->save($data);
        $rstId = $this->model($this->advClassifyModel)->id;

        if ($rst === false) {
            return auto_failure ($data,'广告位分类添加失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位分类未作改动！');
        }
        return auto_return(['id' => $rstId],'分类添加成功！');
    }

    // 某条广告位分类信息编辑修改
    public function advClassifyEdit($data)
    {
        $cateInfo = $this->dbCateInfo($data);

        if ($data['is_public'] == 1 && $data['pid'] != 0) {
            $checkPublic = $this->checkIsPublic($data['pid']);
            if ($checkPublic === false) {
                return auto_failure ($data,'该分类的父级为私有分类，私有分类下不可创建公共分类！');
            }
        }
        $data['update_time'] = time();
        $rst = $this->model($this->advClassifyModel)
                    ->where('id','in',$data['id'])
                    ->update($data);
        if ($rst === false) {
            return auto_failure ($data,'分类信息更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类信息未作改动！');
        }

        if ($data['pid'] == 0) {
            if ($data['other_app_read'] != $cateInfo['other_app_read']) {
                $res = $this->model($this->advClassifyModel)
                    ->where('app_id','in',$data['app_id'])
                    ->update(['other_app_read' => $data['other_app_read']]);
                if ($res === false) {
                    return auto_failure ($data,'子分类可读应用信息更改失败！');
                } elseif ($rst === 0) {
                    return auto_failure ($data,'子分类可读应用信息未作改动！');
                }
            }
        }

        return auto_return(['number' => $rst],'分类信息更改成功！');
    }

    // 启用某个分类广告位分类
    public function advClassifyEnable($data): array
    {
        $updateData['status'] = 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->advClassifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }
        return auto_return(['number' => $rst], '分类状态修改成功！');
    }

    // 禁用某个分类广告位分类
    public function advClassifyDisable($data): array
    {
        $updateData['status'] = 0;
        $updateData['update_time'] = time();
        $rst = $this->model($this->advClassifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }
        return auto_return(['number' => $rst], '分类状态修改成功！');
    }

    // 删除某个分类广告位分类
    public function advClassifyDelete($data): array
    {
        $updateData['status'] = -1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->advClassifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }
        return auto_return(['number' => $rst], '分类状态修改成功！');
    }

    // 获取某应用全部广告位分类列表 树状
    public function advClassifyList()
    {
        $_where['status'] = ['neq',-1];
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        $_whereOr = $manageAppId == 11 ? true : '`app_id` = ' . $manageAppId . ' OR FIND_IN_SET(' . $manageAppId . ',`other_app_read`) OR `is_public` = 1';
        $arr = $this->db()->order('id', 'desc')->where($_where)->where($_whereOr)->select();
        foreach ($arr as $k => &$v) {
            $v['status_text'] = $v['status'] == 1 ? '正常' : '禁用';
        }
        //$list = _make_tree(mosaic_child($arr));
        $list = list_to_tree($arr,'id','pid','_child', 0);
        return auto_return($list);
    }

    // 获取某个广告位分类 非删除
    public function advClassifySomeList($data): array
    {
        $condition['id'] = $data['pid'];
        $condition['app_id'] = $data['app_id'];
        $res = $this->db()->where($condition)->find();
        if (empty($res)) {
            return null_data($res,"分类的id和标识不匹配，请重试！");
        }
        $_where['status'] = ['eq',1];
        $_where['pid'] = $data['pid'];
        $arr = $this->db()->where($_where)->order('id', 'desc')->select();

        $list = mosaic_child($arr);
        return auto_return($list);
    }

    // 不同条件广告位分类列表获取 非删除 分类名称 描述
    public function advClassifyCondition($data): array
    {
        $_where['status'] = array('eq','1');
        $_where['name|description']=array('like',"%".$data['keywords']."%");
        $arr = $this->db()
                    ->where($_where)
                    ->order('id', 'desc')
                    ->paginate($data['listRows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($arr);
    }

    // 获取某一广告位分类详情
    public function classifyInfo($data): array
    {
        $res = $this->db()->where($data)->find();
        if ($res == null) {
            return null_data($res,"未找到该分类，请检查id！");
        }
        return auto_return($res);
    }
}
